package com.lbd.system.service.impl;

import com.lbd.base.service.BaseServiceImpl;
import com.lbd.system.bean.UserRole;
import com.lbd.system.dao.UserRoleRepository;
import com.lbd.system.service.UserRoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by WangYajie on 2016/9/19.
 */
@Service
@Transactional
public class UserRoleServiceImpl extends BaseServiceImpl<UserRole, UserRoleRepository> implements UserRoleService {
    @Autowired
    UserRoleRepository userRoleRepository;

    @Override
    public String buildJpql(UserRole userRole, Map<String, Object> parameter) {
        StringBuilder jpqlBuilder = new StringBuilder(" from  UserRole t ");
        if (userRole != null) {
        /*    if (StringUtils.isNotBlank(menu.getName())) {
                jpqlBuilder.append(" and  t.name like :name");
                parameter.put("name", "%" + menu.getName() + "%");
            }
            if (StringUtils.isNotBlank(menu.getCode())) {
                jpqlBuilder.append(" and  t.code like :code");
                parameter.put("code", "%" + menu.getCode() + "%");
            }*/
        }
        return jpqlBuilder.toString();
    }


    @Override
    public List<UserRole> findByEmpId(String id) {
        return userRoleRepository.findByEmpId(id);
    }

    @Override
    public void assign(String uid, String[] ids) {

        userRoleRepository.deleteByEmpId(uid);
        if (ids != null && StringUtils.isNotBlank(uid)) {
            if (ids.length > 0) {
                for (int i = 0; i < ids.length; i++) {
                    UserRole userRole = new UserRole();
                    userRole.setRoleId(ids[i]);
                    userRole.setEmpId(uid);
                    userRoleRepository.save(userRole);
                }
            }
        }
    }
}
